<?php

namespace App\Admin\Extensions\Tools;

use Encore\Admin\Admin;
use Encore\Admin\Grid\Tools\AbstractTool;

class UpdateWechatMenu extends AbstractTool
{
    public function __construct()
    {
    }

    protected function script()
    {

        return <<<EOT
$('.btn-publish').unbind('click').click(function() {
    var id = $(this).data('id');

    swal({
        title: "确认发布?",
        type: "warning",
        showCancelButton: true,
        confirmButtonColor: "#DD6B55",
        confirmButtonText: "确认",
        showLoaderOnConfirm: true,
        cancelButtonText: "取消",
        preConfirm: function() {
            return new Promise(function(resolve) {
                $.ajax({
                    method: 'post',
                    url: '/admin/wechatmenu/publish',
                    data: {
                        _method:'post',
                        _token:LA.token,
                        id:id
                    },
                    success: function (data) {
                        $.pjax.reload('#pjax-container');

                        resolve(data);
                    }
                });
            });
        }
    }).then(function(result) {
        var data = result.value;
        if (typeof data === 'object') {
            if (data.status) {
                swal(data.message, '', 'success');
            } else {
                swal(data.message, '', 'error');
            }
        }
    });
});

EOT;
    }

    public function render()
    {
        Admin::script($this->script());

        return view('admin.tools.update_wechat_menu');
    }
}
